#include "drv_wifi.h"
void usart_sendstr(USART_TypeDef * USARTx,char* str,uint8_t size)
{
	char temp[20];
	uint8_t i=0;
	USART_ClearFlag(USARTx,USART_FLAG_TC);
	for(i=0;i<size;i++)
	{
		USART_SendData(USARTx,*str);
		temp[i]=*str;
		while(USART_GetFlagStatus(USARTx,USART_FLAG_TC));
		USART_ClearFlag(USARTx,USART_FLAG_TC);
		str++;
	}
	temp[i+1]='\0';
	printf("temp=%s\r\n",temp);
}
void drv_wifi_usart(void)
{
	GPIO_InitTypeDef  GPIO_InitStruct;
	USART_InitTypeDef USART_InitStruct;
	NVIC_InitTypeDef  NVIC_InitStruct;

	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE);
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE);
	
	GPIO_InitStruct.GPIO_Mode=GPIO_Mode_AF;
	GPIO_InitStruct.GPIO_OType=GPIO_OType_PP;
	GPIO_InitStruct.GPIO_Pin=GPIO_Pin_11|GPIO_Pin_10;
	GPIO_InitStruct.GPIO_PuPd=GPIO_PuPd_UP;
	GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_Init(GPIOB,&GPIO_InitStruct);

	GPIO_PinAFConfig(GPIOB,GPIO_PinSource10,GPIO_AF_USART3);
	GPIO_PinAFConfig(GPIOB,GPIO_PinSource11,GPIO_AF_USART3);

	USART_InitStruct.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
	USART_InitStruct.USART_BaudRate=115200;
	USART_InitStruct.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;
	USART_InitStruct.USART_Parity=USART_Parity_No;
	USART_InitStruct.USART_StopBits=USART_StopBits_1;
	USART_InitStruct.USART_WordLength=USART_WordLength_8b;
	USART_Init(USART3,&USART_InitStruct);

	USART_ITConfig(USART3,USART_IT_RXNE,ENABLE);
	USART_ITConfig(USART3,USART_IT_IDLE,ENABLE);

	NVIC_InitStruct.NVIC_IRQChannel=USART3_IRQn;
	NVIC_InitStruct.NVIC_IRQChannelCmd=ENABLE;
	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority=0;
	NVIC_InitStruct.NVIC_IRQChannelSubPriority=1;
	
	NVIC_Init(&NVIC_InitStruct);

	USART_ClearFlag(USART3,USART_FLAG_TC);
	USART_Cmd(USART3,ENABLE);
}
void drv_wifi_init(void)//PB10->TX			PB11->RX
{ 
	memset(recv_wifi,0,sizeof(recv_wifi));
	
	drv_systick_ms(2000);
	
	usart_sendstr(USART3,"AT\r\n",strlen("AT\r\n"));
	
	drv_systick_ms(2000);
	printf("recv=%s\r\n",recv_wifi);
//	while(strcmp(recv_wifi,"OK\r\n")!=0);
	
//	memset((void*)recv_wifi,0,50);
//	printf("wifi!");
//	usart_sendstr(USART3,"AT+CWMODE=2\r\n",13);	
//	
//	while(strcmp((char*)recv_wifi,"OK\r\n")!=0);
//	memset((void*)recv_wifi,0,50);
//	
//	usart_sendstr(USART3,"AT+CIPMUX=1\r\n",13);

//	while(strcmp((char*)recv_wifi,"OK\r\n")!=0);
//	memset((void*)recv_wifi,0,50);
//		
//	usart_sendstr(USART3,"AT+CWSAP_DEF=\"Zydsg13\",\"12345678\",5,3\r\n",39);	
//	
//	while(strcmp((char*)recv_wifi,"OK\r\n")!=0);
//	memset((void*)recv_wifi,0,50);

//	usart_sendstr(USART3,"AT+CIPAP=\"192.168.4.1\",\255.255.255.0\",\"192.168.4.1\"\r\n",13);

//	while(strcmp((char*)recv_wifi,"OK\r\n")!=0);
//	memset((void*)recv_wifi,0,50);

//	usart_sendstr(USART3,"AT+CIPSERVER=1,8080\r\n",21);

//	while(strcmp((char*)recv_wifi,"OK\r\n")!=0);
//	memset((void*)recv_wifi,0,50);
//	
//	while(strcmp((char*)recv_wifi,"0,CONNECT\r\n")!=0);
//	memset((void*)recv_wifi,0,50);
}
void  drv_wifi(void)
{
	if(strcmp((char*)recv_wifi,"+IPD,0,6:open\r\n")==0)
	{
		strcpy((char*)recv,"open\r\n");
		ready_char=1;	
		memset((void*)recv_wifi,0,50);
	}
	else if(strcmp((char*)recv_wifi,"+IPD,0,6:clos\r\n")==0)
	{
		strcpy((char*)recv,"clos\r\n");
		ready_char=1;
		memset((void*)recv_wifi,0,50);
	}
		else if(strcmp((char*)recv_wifi,"+IPD,0,5:run\r\n")==0)
	{
		strcpy((char*)recv,"run\r\n");
		ready_char=1;	
		memset((void*)recv_wifi,0,50);
	}
		else if(strcmp((char*)recv_wifi,"+IPD,0,3:!\r\n")==0)
	{
		strcpy((char*)recv,"!\r\n");
		ready_char=1;	
		memset((void*)recv_wifi,0,50);
	}
		else if(strcmp((char*)recv_wifi,"+IPD,0,7:stand\r\n")==0)
	{
		strcpy((char*)recv,"stand\r\n");
		ready_char=1;	
		memset((void*)recv_wifi,0,50);
	}
		else if(strcmp((char*)recv_wifi,"+IPD,0,5:lie\r\n")==0)
	{
		strcpy((char*)recv,"lie\r\n");
		ready_char=1;	
		memset((void*)recv_wifi,0,50);
	}
		else if(strcmp((char*)recv_wifi,"+IPD,0,6:temp\r\n")==0)
	{
		strcpy((char*)recv,"temp\r\n");
		ready_char=1;	
		memset((void*)recv_wifi,0,50);
	}
		else if(strcmp((char*)recv_wifi,"+IPD,0,3:~\r\n")==0)
	{
		strcpy((char*)recv,"~\r\n");
		ready_char=1;	
		memset((void*)recv_wifi,0,50);
	}

}
